<template>
  <div class="dynamicBi-container">
    <div v-show="!showMaster">
      <el-card class="box-card dynamicSearch" ref="searchCard">
        <dynamicSearch
          ref="dySearch"
          :searchData="dynamicData.searchData"
          @postForm="getList"
          @resetSearch="getRetrieve"
          :isFloatRight="false"
        ></dynamicSearch>
      </el-card>
      <el-card class="box-card dynamicContext" ref="contextCard">
        <biDynamicList
          ref="dyList"
          :viewData="dynamicData.viewData"
          :addFormData="dynamicData.addFormData"
          :queryParams="queryParams"
          :showQueryBtn="showQueryBtn"
          @updateValue="passToMaster"
          @switchShowMaster="switchShowMaster"
          class="dynamicList"
        ></biDynamicList>
      </el-card>
    </div>
    <div class="fullHeight" v-show="showMaster">
      <spsMaster class="fullHeight setFlexBox spsMasterDialog" ref="spsMaster" :queryParams="selectedRow" @back="switchShowMaster"></spsMaster>
    </div>
  </div>
</template>

<script>
import biDynamicList from '@/components/SpsPublic/Dynamic/biDynamicList'
import dynamicSearch from '@/components/SpsPublic/Dynamic/dynamicSearch'
import dynamicAddForm from '@/components/SpsPublic/Dynamic/dynamicAddForm'
import spsMaster from '@/components/SpsPublic/Master/SpsMaster'
import { baseListRetrieveCondition } from '@/api/mis/retrievecondition'
import { listMisView } from '@/components/SpsPublic/Dynamic/misView.js'
import { getBaseFunction } from '@/api/mis/function'
import store from '@/store/index'
import cacheDestory from '@/utils/mixins/cacheDestory'

export default {
  name: 'queryDynamicBi',
  mixins: [cacheDestory],
  components: {
    biDynamicList,
    dynamicSearch,
    dynamicAddForm,
    spsMaster,
  },
  data() {
    return {
      // 显示查看按钮
      showQueryBtn: true,
      store: store,
      dynamicData: {
        searchData: [],
        viewData: [],
        addFormData: {
          title: '',
          formData: [],
        },
        updateData: {},
        handleType: 'view',
        conType: {
          datePicker: 'el-date-picker',
          treeSelect: 'mis-select-tree',
        },
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        queryType: 'Q',
      },
      selectedRow: {
        selectedCt: null,
        functionData: {},
      },
      showMaster: false,
      funNo: null,
      dispSearchData: null,
      routeQuery: null,
    }
  },
  created() {
    this.init()
  },
  beforeRouteLeave(to, from, next) {
    next()
  },
  methods: {
    // 初始化
    init() {
      const that = this
      this.funNo = this.$route.query.funNo
      this.routeQuery = this.$route.query
      this.selectedRow.funNo = this.funNo
      this.queryParams.ctFunNo = this.funNo
      // 请求获取搜索查询信息数据
      this.getRetrieve()
      // 获取table展示列数据
      this.getMisView()
      store.dispatch('createSingleStore', this.funNo)
    },
    // 获取funNo、bussId
    getParam() {
      // 非子表信息，从URL参数中获取
      const query = this.routeQuery
      for (let element in query) {
        this.queryParams[element] = query[element]
      }
    },
    // 获取该功能的数据
    getFunctionData() {
      const promise = new Promise((resolve, reject) => {
        getBaseFunction(this.funNo).then((response) => {
          this.selectedRow.functionData = response.data
          this.showQueryBtn =
            response.data.ctUrl != '无' && response.data.ctUrl != null
          const pageSize = response.data.amRow
          if (pageSize && pageSize > 0 && pageSize <= 100) {
            this.queryParams.pageSize = pageSize
          }
          resolve()
        })
      })
      const that = this
      promise.then(() => {
        that.$refs.dySearch.postForm()
      })
    },

    // 传值至spsMaster
    passToMaster(value) {
      this.selectedRow.selectedCt = value
      this.switchShowMaster(false)
      this.$refs['spsMaster'].resetTabSelected()
      this.$refs['spsMaster'].refresh()
    },
    // 切换master显示
    switchShowMaster(refreshFlag) {
      // master中数据保存，则刷新列表
      if (refreshFlag) {
        this.$refs.dyList.getList()
      }
      this.showMaster =
        store.getters.singleStore['main' + this.selectedRow.funNo].showMaster
    },
    // 获取表格显示列
    getMisView() {
      this.loading = true
      listMisView(this.queryParams).then((response) => {
        this.dynamicData.viewData = response.rows
        this.dynamicData.viewData = this.dynamicData.viewData.filter(
          (column) => {
            return column.ctIfTable == '1'
          }
        )
        this.loading = false
      })
    },
    // 获取搜索查询信息数据
    getRetrieve() {
      const that = this
      baseListRetrieveCondition(that.queryParams).then((response) => {
        that.dynamicData.searchData = response.rows
        // 包含下级元素的位置
        let index = -1
        for (let i = 0; i < that.dynamicData.searchData.length; i++) {
          const item = that.dynamicData.searchData[i]
          if (
            item.ctConType == that.dynamicData.conType.treeSelect &&
            item.ctConTreetype == '99' &&
            item.ctConDisp
          ) {
            index = i + 1
          }
          if (that.routeQuery[item.ctColName]) {
            item.ctColDefaultv = that.routeQuery[item.ctColName]
            that.queryParams[item.ctColName] = item.ctColDefaultv
          } else {
            item.ctColDefaultv = null
            that.queryParams[item.ctColName] = null
          }
        }
        console.log(that.dynamicData.searchData)
        if (index !== -1) {
          const retrieve = {
            ctConType: 'el-checkbox',
            ctColDefaultv: true,
            ctColName: 'includeFlag',
            ctColTitle: '',
            ctConDisp: true,
          }
          that.dynamicData.searchData.splice(index, 0, retrieve)
        }
        // that.queryParams = that.dynamicData.searchData
        // 获取该功能的数据
        that.getFunctionData()
      })
    },
    // 查询表格数据
    getList(val) {
      this.getParam()
      var keys = Object.keys(val)
      for (let i = 0; i < keys.length; i++) {
        if (val[keys[i]] !== undefined && val[keys[i]] != null) {
          this.queryParams[keys[i]] = val[keys[i]]
        }
      }
      this.$refs['dyList'].getList()
    },
  },
}
</script>

<style scoped lang="scss">
.box-card {
  /* padding: 5px; */
  margin: 4px 10px 4px 10px;
}
.dynamicSearch {
  overflow: visible;
}

.card-bread-crumb {
  margin-left: 11px;
  margin-bottom: 3px;
}

.dynamicList {
  position: relative;
}
.spsMasterDialog{
  ::v-deep .el-tabs{
    flex:1;
    display: flex;
    flex-direction: column;
    .el-tabs__content{
      flex:1;
      .el-tab-pane{
        height: 100% !important;
        .div-height{
            height: 100% !important;
            min-height: auto !important;
            .vif{
              height: 100%;
              .default_vue{
                height: 100% !important;
              }
            }
          }
      }
    }
  }

}
</style>
